﻿//JumbotCms_4.1 Final
using System;
using System.Web;
using System.Data;
using Jumbot.Common;
using JumbotCms.Common;
namespace JumbotCms.WebFile.MTag
{
    public partial class _view_threadmanage_ajax : JumbotCms.API.UserCenter
    {
        private string _operType = string.Empty;
        private string _response = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!CheckFormUrl())
            {
                Response.Write("提交有误");
                Response.End();
            }

            this._operType = q("oper");
            switch (this._operType)
            {
                case "ajaxDelReply":
                    ajaxDelReply();
                    break;
                case "ajaxManage":
                    ajaxManage();
                    break;
                case "ajaxGetList":
                    ajaxGetList();
                    break;
                case "ajaxTopList":
                    ajaxTopList();
                    break;
                default:
                    DefaultResponse();
                    break;
            }
            Response.Write(this._response);
        }
        private void DefaultResponse()
        {
            User_Load("", "json");
        }
        private void ajaxDelReply()
        {
            User_Load("", "json");
            string rId = f("id");
            doh.Reset();
            doh.ConditionExpress = "id=@id and UserId=" + UserId;
            doh.AddConditionParameter("@id", rId);
            if (doh.Delete("jcms_normal_mtag_thread_reply") == 1)
                this._response = JsonResult(1, "成功删除");
            else
                this._response = JsonResult(0, "删除有误");
        }
        private void ajaxManage()
        {
            User_Load("", "json");
            string act = q("act");
            string ids = f("ids");
            if (BatchThreadManage(act, ids, UserId, Str2Str(q("mtagid"))))
                this._response = JsonResult(1, "操作成功");
            else
                this._response = JsonResult(0, "操作失败");
        }
        private void ajaxGetList()
        {
            User_Load("", "json");
            int page = Int_ThisPage();
            int PSize = Str2Int(q("pagesize"), 10);
            int IsGood = Str2Int(q("isgood"));
            int IsHead = Str2Int(q("ishead"));
            int countNum = 0;
            string sqlStr = "";
            string joinStr = "a.UserId=B.Id";
            string whereStr1 = "A.mtagid=" + Str2Str(q("mtagid"));
            string whereStr2 = "mtagid=" + Str2Str(q("mtagid"));
            if (IsGood == 1)
            {
                whereStr1 += " and A.isgood=1";
                whereStr2 += " and isgood=1";
            }
            if (IsHead == 1)
            {
                whereStr1 += " and A.ishead=1";
                whereStr2 += " and ishead=1";
            }
            doh.Reset();
            doh.ConditionExpress = whereStr2;
            countNum = doh.Count("jcms_normal_mtag_thread");
            string selectFields = "a.id as threadid," +
                "a.UserId as threaduserid," +
                "a.Title as threadTitle," +
                "a.MTagId as threadMTagId," +
                "a.IsGood as threadIsGood," +
                "a.IsHead as threadIsHead," +
                "a.ReplyCount as threadReplyCount," +
                "a.AddTime as threadAddTime," +
                "b.UserName as threadusername," +
                "(select top 1 userid from [jcms_normal_mtag_thread_reply] where threadid=a.id order by addtime desc) as lastreplyuserid," +
                "(select top 1 addtime from [jcms_normal_mtag_thread_reply] where threadid=a.id order by addtime desc) as lastreplytime," +
                "(select username from [jcms_normal_user] where id =(select top 1 userid from [jcms_normal_mtag_thread_reply] where threadid=a.id order by addtime desc)) as lastreplyusername";
            sqlStr = Jumbot.Common.staticFunction.GetSql(selectFields, "jcms_normal_mtag_thread", "jcms_normal_user", "Id", PSize, page, "desc", joinStr, whereStr1, whereStr2);
            doh.Reset();
            doh.SqlCmd = sqlStr;
            DataTable dt = doh.GetDataTable();
            this._response = "{result :\"1\"," +
                "returnval :\"操作成功\"," +
                "pagerbar :\"" + Jumbot.Common.HtmlPager.GetPageBar(3, "js", 2, countNum, PSize, page, "javascript:ajaxList(<#page#>);") + "\"," +
                Jumbot.Common.JsonHelp.DtToSON(dt, (PSize * (page - 1))) +
                "}";
            dt.Clear();
            dt.Dispose();
        }
        private void ajaxTopList()
        {
            User_Load("", "json");
            int page = Int_ThisPage();
            int PSize = Str2Int(q("pagesize"), 10);
            int countNum = 0;
            string sqlStr = "";
            string joinStr = "a.UserId=B.Id";
            string whereStr1 = "A.Id>0";
            string whereStr2 = "Id>0";
            if (Str2Str(q("ismine")) == "1")
            {
                whereStr1 += " and A.MTagId in (select MTagId from [jcms_normal_mtag_member] where UserId=" + UserId + ")";
                whereStr2 += " and MTagId in (select MTagId from [jcms_normal_mtag_member] where UserId=" + UserId + ")";
            }
            doh.Reset();
            doh.ConditionExpress = whereStr2;
            countNum = doh.Count("jcms_normal_mtag_thread");
            string selectFields = "a.id as threadid," +
                "a.UserId as threaduserid," +
                "a.Title as threadTitle," +
                "a.MTagId as threadMTagId," +
                "a.IsGood as threadIsGood," +
                "a.IsHead as threadIsHead," +
                "a.ReplyCount as threadReplyCount," +
                "a.AddTime as threadAddTime," +
                "b.UserName as threadusername," +
                "(select id from [jcms_normal_mtag] where id=a.mtagid) as threadmtagid," +
                "(select title from [jcms_normal_mtag] where id=a.mtagid) as threadmtagname," +
                "(select top 1 userid from [jcms_normal_mtag_thread_reply] where threadid=a.id order by addtime desc) as lastreplyuserid," +
                "(select top 1 addtime from [jcms_normal_mtag_thread_reply] where threadid=a.id order by addtime desc) as lastreplytime," +
                "(select username from [jcms_normal_user] where id =(select top 1 userid from [jcms_normal_mtag_thread_reply] where threadid=a.id order by addtime desc)) as lastreplyusername";
            sqlStr = Jumbot.Common.staticFunction.GetSql(selectFields, "jcms_normal_mtag_thread", "jcms_normal_user", "Id", PSize, page, "desc", joinStr, whereStr1, whereStr2);
            doh.Reset();
            doh.SqlCmd = sqlStr;
            DataTable dt = doh.GetDataTable();
            this._response = "{result :\"1\"," +
                "returnval :\"操作成功\"," +
                "pagerbar :\"" + Jumbot.Common.HtmlPager.GetPageBar(3, "js", 2, countNum, PSize, page, "javascript:ajaxList(<#page#>);") + "\"," +
                Jumbot.Common.JsonHelp.DtToSON(dt, (PSize * (page - 1))) +
                "}";
            dt.Clear();
            dt.Dispose();
        }
    }
}
